#include "seqList.h"

void SeqListPrint(const SeqList *psl)
{
    assert(psl);
    for (int i = 0; i < psl->size; ++i)
    {
        printf("%d ", psl->s[i]);
    }
    printf("\n");
}

void SeqListInit(SeqList *psl)
{
    psl->s = NULL;
    psl->size = 0;
    psl->capacity = 0;
}

void SeqListDestroy(SeqList *psl)
{
    free(psl->s);
    psl->s = NULL;
    psl->size = 0;
    psl->capacity = 0;
}

void SeqListCheckCapacity(SeqList *psl)
{
    //判空
    assert(psl);
    //判断容量是否已满
    if (psl->size == psl->capacity)
    {
        size_t newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
        SLDateType *tmp = (SLDateType *)realloc(psl->s, sizeof(SLDateType) * newCapacity);
        //判断tmp是否为空->为空说明堆区已满，自动结束程序
        if (tmp == NULL)
        {
            printf("realloc fail\n");
            exit(EXIT_FAILURE);
        }
        else
        {
            psl->s = tmp;
            psl->capacity = newCapacity;
        }
    }
}

void SeqListPushBack(SeqList *psl, SLDateType x)
{
    assert(psl);
    SeqListCheckCapacity(psl);
    psl->s[psl->size] = x;
    psl->size++;
}

void SeqListPopBack(SeqList *psl)
{
    assert(psl);
    //如果不判断，则为向左(负数)溢出
    if (psl->size > 0)
    {
        psl->size--;
    }
}

void SeqListPushFront(SeqList *psl, SLDateType x)
{
    assert(psl);

    int end = psl->size - 1;
    //挪动数据，腾出头部([0])空间
    while (end >= 0)
    {
        psl->s[end + 1] = psl->s[end];
        end--;
    }
    psl->s[0] = x;
    psl->size++;
}

void SeqListPopFront(SeqList *psl)
{
    assert(psl);
    if (psl->size > 0)
    {
        int begin = 1;
        int end = psl->size;
        while (begin < end)
        {
            psl->s[begin - 1] = psl->s[begin];
            ++begin;
        }
        --psl->size;
    }
}

void SeqListInsert(SeqList *psl, size_t pos, SLDateType x)
{
    assert(psl);
    if (pos < psl->size)
    {
        size_t end = psl->size;
        while (end > pos)
        {
            psl->s[end] = psl->s[end - 1];
            --end;
        }
        psl->s[pos] = x;
        psl->size++;
    }
    else
    {
        printf("error!!!\n");
    }
}

void SeqListErase(SeqList *psl, size_t pos)
{
    assert(psl);
    assert(psl->size > pos);

    int begin = pos + 1;
    while (begin < psl->size)
    {
        //覆盖pos位置的内容
        psl->s[begin - 1] = psl->s[begin];
        ++begin;
    }
    psl->size--;
}

int SeqListFind(SeqList *psl, int x)
{
    assert(psl);
    for (int i = 0; i < psl->size; ++i)
    {
        if (psl->s[i] == x)
        {
            return i;
        }
    }
    return -1;
}

int main()
{
    SeqList sq1;
    SeqListInit(&sq1);
    SeqListPushBack(&sq1, 1);
    SeqListPushBack(&sq1, 2);
    SeqListPushBack(&sq1, 3);
    SeqListPushBack(&sq1, 4);
    SeqListPushBack(&sq1, 5);

    SeqListPushFront(&sq1, 10);
    SeqListPushFront(&sq1, 15);
    SeqListPushFront(&sq1, 20);

    SeqListPopFront(&sq1);
    SeqListPopFront(&sq1);
    SeqListPopFront(&sq1);

    SeqListInsert(&sq1, 1, 100);

    SeqListErase(&sq1, 2);

    int find = SeqListFind(&sq1, 100);
    SeqListPrint(&sq1);
    if (find == -1)
    {
        printf("没找到！！！\n");
    }
    else
    {
        printf("找到了，下标为[%d]\n", find);
    }
    system("pause");
    return 0;
}